﻿'use strict';

define([
], function () {
    var Model = Backbone.Model.extend({
        superClass: null,

        //Static
        defaults: {
            priceAdjPercentage: null,
            priceAdjType: '',
            priceAdjusted: false,
        },
        priceAdjustTypes: {
            profit: 'Profit',
            total: 'Total',
        },

        constructor: function () {
            this.superClass = Model.__super__;

            this.superClass.constructor.apply(this, arguments);
        },
        initialize: function () {
            this.listenTo(this, 'change:priceAdjPercentage change:priceAdjType', this._change, this);
        },
        
        get: function (attr) {
            var retVal;

            if (attr) {
                var attrFixed = $.trim(attr).toUpperCase();

                switch (attrFixed) {
                    case "PRICEADJINDEX":
                        var perc = this.get('priceAdjPercentage');

                        switch (this.get('priceAdjType'))
                        {
                            case this.priceAdjustTypes.profit:
                                retVal = 1/(1 - perc / 100);
                                break;
                            case this.priceAdjustTypes.total:
                                retVal = 1 + perc / 100;
                                break;
                            default:
                                retVal = null;
                                break;
                        }
                        break;
                    default:
                        retVal = this.superClass.get.call(this, attr);
                        break;
                }
            }
            else {
                retVal = this.superClass.get.call(this, attr);
            }

            return retVal;
        },
        set: function (key, val, options) {
            if (key == null) return this;

            var attrs;

            // Handle both `'key', value` and `{key: value}` -style arguments.
            if (typeof key === 'object') {
                attrs = key;
                options = val;
            } else {
                (attrs = {})[key] = val;
            }

            if (!options)
                options = {};

            var priceAdjustedCurrent = this.get('priceAdjusted'),
                priceAdjustedNew = attrs.priceAdjusted,
                priceAdjustedSet = (typeof attrs.priceAdjusted !== 'undefined');

            var priceAdjustedChanging = ((priceAdjustedSet == true) && (priceAdjustedNew != priceAdjustedCurrent));


            if (priceAdjustedChanging == true) {
                if (priceAdjustedNew == false) {
                    attrs.priceAdjPercentage = this.defaults.priceAdjPercentage;
                    attrs.priceAdjType = this.defaults.priceAdjType;
                }
            }


            this.superClass.set.call(this, attrs, options);
        },
        toJSON: function () {
            return _.extend(
                this.superClass.toJSON.call(this),
                {
                    priceAdjIndex: this.get('priceAdjIndex'),
                }
            );
        },

        _change: function (model, options) {
            this.trigger('change:priceAdjIndex');
        },
    });

    return Model;
});